package workforce.limousine.struts;

import limousine.struts.form.ProfileForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;


import limousine.vo.Users;
import limousine.dbservice.UsersService;
import limousine.util.Config;
import limousine.util.Service;
import limousine.util.Token;
import limousine.util.Func;
import limousine.util.Mail;

public class UsersInsert extends Action {

    private String errorMsg = "Process Fail";
    private Func func = new Func();

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        HttpSession session = request.getSession();
        System.out.println("***UsersInsert");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /*if (!user.getRole_name().equals("Admin")) {
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/


        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        Users o = new Users();
        UsersService ms = new UsersService();
        ms.setDataSource(ds);



        ProfileForm f = (ProfileForm) form;

        //		check systems name exist
        o = ms.getByUserName(f.getUserName());
        if (o != null) {//exist
            errorMsg = "This user exist";
             session.setAttribute(Token.MSG_ERROR, errorMsg);
            return mapping.findForward(Token.FAIL);
        }
//		check systems name exist


        o = new Users();
        o.setId(f.getId());
        o.setUserName(f.getUserName());
        o.setPassword(func.protectNullString(f.getPassword()));
        o.setName(func.protectNullString(f.getName()));
        o.setPhone(func.protectNullString(f.getPhone()));
        o.setGender(f.getGender());
        
        o.setEmail(func.protectNullString(f.getEmail()));
        
        o.setEnable(f.getEnable());
        o.setRoleId(f.getRoleId());

        
        o.setPreviewImage(func.protectNullString(f.getPreviewImage()));
        o.setStatus(Integer.toString(Config.USER_STATUS_OFFLINE));
        o.setCreateBy(user.getId());
        

        if (ms.insert(o)) {

            UsersService uss = new UsersService();
            uss.setDataSource(ds);

            /*
            //send Mail
            o = uss.getByUserName(o.getUserName());
            String x = "กรุณากด <a href='" + service.getUrlFullContextPath(request) + "/activate.do?id=" + o.getId() + "&code=" + o.getPassword() + "' >Activate</a> เพื่อยืนยันการเป็นสมาชิกค่ะ<br>";
            Mail.sendSSLMessage(o.getEmail(), Config.MAIL_SUBJECT_ACTIVE, x, Config.MAIL_FORM);
            //send Mail
            */
            
            session.setAttribute(Token.MSG_USER, Token.MSG_INSERT_SUCCESS);
            return mapping.findForward(Token.SUCCESS);
        } else {
            session.setAttribute(Token.MSG_ERROR, errorMsg);
            return mapping.findForward(Token.FAIL);
        }
    }

    private String checkEmptyString(String value) {// optimize data
        value = (value == null) ? "" : value.trim();
        return func.jsp2UTF(value);
    }

}
